<?php

class Silk_Tms_Block_Adminhtml_Car_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

		public function __construct()
		{
				parent::__construct();
				$this->setId("carGrid");
				$this->setDefaultSort("id");
				$this->setDefaultDir("DESC");
				$this->setSaveParametersInSession(true);
		}

		protected function _prepareCollection()
		{
			
			$collection = Mage::getModel("tms/car")->getCollection();
			$websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
			if($websiteId){
				$collection->addFieldToFilter("website_id",$websiteId);
			}
			$this->setCollection($collection);
			return parent::_prepareCollection();
		}
		protected function _prepareColumns()
		{
				$this->addColumn("id", array(
				"header" => Mage::helper("tms")->__("ID"),
				"align" =>"right",
				"width" => "100px",
			    "type" => "number",
				"index" => "id",
				));
				$type = array("1"=>"大","2"=>"中","3"=>"小");
				$this->addColumn("car_type", array(
				"header" => Mage::helper("tms")->__("类型"),
				"index" => "car_type",
				'type'  => 'options',
				'width' => '200px',
				'align' => 'center',
				'options' => $type
				));
				$this->addColumn("number", array(
				"header" => Mage::helper("tms")->__("车牌号"),
				"index" => "number",
				'align' => 'center',
				));
				$this->addColumn('website_id', array(
				    'header'    => Mage::helper('tms')->__('Website'),
				    'align'     => 'center',
				    'width'     => '80px',
				    'type'      => 'options',
				    'options'   => $this->getWebsite(),
				    'index'     => 'website_id',
				));

				
				$this->addColumn('action',
					array(
						'header'   =>   Mage::helper('tms')->__('Action'),
						'type'     =>   'action',
						'getter'   =>   'getId',
						'align' => 'center',
						'actions'  =>   array(
							array(
								'caption' =>  Mage::helper('tms')->__('编辑'),
								'url'     => array(
								    'base'=>'*/*/edit',
								    'params'=>array('id'=>$this->getRequest()->getParam('id'))
								),
								'field'   =>  'id'
							),
						 ),
						'filter'      =>  false,
						'sortable'    =>  false,
						'width' => '100px',
 					)
				);

				return parent::_prepareColumns();
		}

		public function getRowUrl($row)
		{
			   return $this->getUrl("*/*/edit", array("id" => $row->getId()));
		}



		
		protected function _prepareMassaction()
		{
			$this->setMassactionIdField('id');
			$this->getMassactionBlock()->setFormFieldName('ids');
			$this->getMassactionBlock()->setUseSelectAll(true);
			$this->getMassactionBlock()->addItem('remove_exchange', array(
					 'label'=> Mage::helper('tms')->__('Remove'),
					 'url'  => $this->getUrl('*/*/massRemove'),
					 'confirm' => Mage::helper('tms')->__('Are you sure?')
				));

			return $this;
		}

		protected function getWebsite()
		{
		    $websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
		    $website = Mage::getModel('core/website')->getCollection()->toOptionHash();
		    $outs= array();
		    if (key_exists($websiteId,$website))
		    {
		      $websitename = $website[$websiteId];
		      $outs[$websiteId]=$websitename;
		      return $outs;
		    }
		      return $website;
		}
			

}